package activitis;

import java.util.List;

import utils.BaseAsync;
import utils.Constantes;
import utils.PreferencesHelper;

import com.example.rapichofer.R;
import com.google.gson.Gson;

import controladores.ControladorMoviles;

import entidades.Movil;

import adaptadores.MovilArrayAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class SeleccionMovilActivity extends Activity
{
	public static int SELECCION_MOVIL = 2202;
	
	private ListView list;
	private MovilArrayAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_seleccion_movil);
		
		list = (ListView) findViewById(R.id.listaMoviles);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.seleccion_movil, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		int id = item.getItemId();
		
		if (id == R.id.nuevoMovil)
		{
			Intent intent = new Intent (SeleccionMovilActivity.this, RegistroMovilActivity.class);
			startActivityForResult(intent, RegistroMovilActivity.REGISTRO_MOVIL);
			
			return true;
		}
		else
			return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		new BuscarMoviles().execute();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == RegistroMovilActivity.REGISTRO_MOVIL && resultCode == Activity.RESULT_OK)
		{
			String movilString = data.getStringExtra(Constantes.RegistroUsuario.MOVIL);
			
			if (movilString != null)
			{
				Movil movil = new Gson().fromJson(movilString, Movil.class);
				
				new RegistrarMovil(movil).execute();
			}
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private class ListenerLista implements OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long seleccion)
		{
			Intent intent = new Intent();
			intent.putExtra(Constantes.DataBundle.MOVIL, new Gson().toJson(adapter.getMovil((int) seleccion)));
			
			setResult(Activity.RESULT_OK, intent);
			finish();
		}
	}
	
	private class BuscarMoviles extends BaseAsync
	{
		private List<Movil> moviles;
		
		public BuscarMoviles ()
		{
			super (SeleccionMovilActivity.this, "Buscando moviles...");
		}
		
		@Override
		protected Void doInBackground(Void... arg0)
		{
			moviles = new ControladorMoviles().getMovilesChofer(new PreferencesHelper(getApplicationContext()).leerID());
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result)
		{
			super.onPostExecute(result);
			
			if (moviles != null)
			{
				adapter = new MovilArrayAdapter(SeleccionMovilActivity.this, moviles);
				list.setAdapter(adapter);
				list.setOnItemClickListener(new ListenerLista());
			}
			else
			{
				setResult(Activity.RESULT_CANCELED);
				finish();
			}
		}
	}
	
	private class RegistrarMovil extends BaseAsync
	{
		private Movil movil;
		private boolean res;
		
		public RegistrarMovil (Movil movil)
		{
			super (SeleccionMovilActivity.this, "Registrando...");
			
			this.movil = movil;
		}
		
		@Override
		protected Void doInBackground(Void... params)
		{
			res = new ControladorMoviles().registrarMovil(new PreferencesHelper(getApplicationContext()).leerID(), movil);
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result)
		{
			super.onPostExecute(result);
			
			if (res)
				new BuscarMoviles().execute();
			else
				Toast.makeText(SeleccionMovilActivity.this, "Fallo el registro.", Toast.LENGTH_SHORT).show();
		}
	}
}
